package org.example.hrpc.registry;

import org.example.hrpc.common.URL;
import org.example.hrpc.common.utils.ConcurrentHashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Set;

public abstract class AbstractRegistry implements Registry {
    private final  Logger logger = LoggerFactory.getLogger(getClass());

    private final Set<URL> registered = new ConcurrentHashSet<>();

    @Override
    public void register(URL url) {
        if (url == null) {
            throw new IllegalArgumentException("register url == null");
        }
        if (logger.isInfoEnabled()) {
            logger.info("Register: " + url);
        }
        registered.add(url);
        // 模板方法，由子类实现
        doRegister(url);
    }

    @Override
    public void unregister(URL url) {
        if (url == null) {
            throw new IllegalArgumentException("unregister url == null");
        }
        if (logger.isInfoEnabled()) {
            logger.info("Unregister: " + url);
        }
        registered.remove(url);
        doUnregister(url);
    }




    protected abstract void doRegister(URL url);

    protected abstract void doUnregister(URL url);
}
